<template>
  <div class="page-list-live">
    <PageList>
      <template #header>环境监测设备</template>
      <template #filter>
        <ListFilter :form="topFilter" :key-list="keyList" @search="handleSearch" @reset="handleReset"></ListFilter>
      </template>
      <template #table>
        <ListTable ref="ListTable" :loading="tableLoading" :table-data="tableData" :table-col="tableCol" @rowClick="rowClickHandler">
          <template slot="col1" slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              :loading="scope.row.loadingEdit"
              size="mini"
              style="margin-left: 0px; margin-right: 10px"
              type="text"
              @click="handleRowEdit(scope.row)"
            >编辑</el-button>
            <el-button
              :loading="scope.row.loadingDel"
              size="mini"
              type="text"
              icon="el-icon-delete"
              style="color: red; margin-left: 0px; margin-right: 10px"
              @click="handleRowDel(scope.row)"
            >删除</el-button>
          </template>
        </ListTable>
        <hr />
        <ListTable ref="RefListTab" :bgcolor="'#FFFAF0'" :loading="tableLoading" :page="false" :table-data="rightTableData" :table-col="rightTableCol">
          <template slot="col1" slot-scope="scope">
            {{ scope.row.minValue }}
            <span>&lt;正常值&lt;</span>
            {{ scope.row.maxValue }}
          </template>
        </ListTable>
      </template>
    </PageList>
    <AddSensor ref="RefAddSensor" @success="afterEditSuccess"></AddSensor>
  </div>
</template>

<script>
  import listMixin from '@/mixins/list'
  import AddSensor from './components/AddSensor.vue'
  import ListFilter from '@/components/ListFilter'
  import { sensorStation } from '@/api/api'
  export default {
    components: {
      AddSensor,
      ListFilter
    },
    mixins: [listMixin],
    data() {
      return {
        topFilter: {
          keyword: { label: '关键字', placeholder: '请输入设备名称或设备编号' }
          // name: { label: '设备名称', placeholder: '请输入设备名称' },
          // code: { label: '设备编号', placeholder: '请输入设备编号' }
        },
        keyList: ['name', 'code'],
        // 左边栏
        tableData: [],
        tableCol: [
          { type: 'index', label: '序号', width: '60px' },
          { prop: 'name', label: '设备名称' },
          { prop: 'code', label: '设备编号' },
          { prop: 'ip', label: '设备IP' },
          { label: '状态', prop: 'status' },
          { label: '操作', custom: 'col1' }
        ],
        // 右边栏
        rightTableData: [],
        rightTableCol: [
          { prop: 'label', label: '监测项' },
          { prop: 'labelValue', label: '编码' },
          { prop: 'unit', label: '单位' },
          { label: '报警设置', custom: 'col1' }
        ]
      }
    },
    methods: {
      rowClickHandler(row, column, event) {
        this.rightTableData = row.settingItems
      },
      // 请求列表数据
      async getList(query) {
        this.tableLoading = true
        try {
          let obj = query || { pageIndex: 1, pageSize: 10, orderBy: 'createdTime', orderByAscent: false }
          const {
            data: { items: resData, count, pageIndex }
          } = await sensorStation('post', obj, false)
          this.tableData = []
          this.tableData = resData.map(item => {
            item.loadingEdit = false
            item.loadingDel = false
            return item
          })
          this.rightTableData = this.tableData[0].settingItems
          // this.rightRow = this.tableData[0]
          this.$nextTick(() => {
            this.$refs['ListTable'].update({ page: pageIndex, count })
            this.tableLoading = false
          })
        } catch (err) {
          this.tableLoading = false
          this.tableData = []
        }
      },
      async afterEditSuccess() {
        this.getList(this.serchObj)
      },
      handleAdd() {
        this.$refs['RefAddSensor'].open()
        this.$refs['RefAddSensor'].getTunnelData()
        this.$refs['RefAddSensor'].getIcon()
        this.$refs['RefAddSensor'].getEquipment()
      },
      handleRowEdit(row) {
        let objData = {
          // 进入哪一个程序
          status: 'edit',
          // 源数据
          row: row,
          // 源id
          id: row.id,
          // 查询参数
          searchData: {
            url: '/EnvironmentTrackingSetting/' + row.id,
            method: 'GET',
            params: null
          }
        }
        this.$refs['RefAddSensor'].getTunnelData()
        this.$refs['RefAddSensor'].getIcon()
        this.$refs['RefAddSensor'].getEquipment()
        this.$refs['RefAddSensor'].openOne(objData)
      },
      async delRowReq(row) {
        try {
          let obj = { id: row.id, value: null }
          await sensorStation('delete', obj, true)
          row.loadingDel = false
          this.getList(this.serchObj)
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        } catch (err) {
          row.loadingDel = false
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        }
      },
      handleRowDel(row) {
        row.loadingDel = true
        this.$_delConfirm(
          '传感器基站',
          () => {
            this.delRowReq(row)
          },
          () => {
            row.loadingDel = false
          }
        )
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
